package practice1_100;

public class Practice64 {
    public int minPathSum(int[][] grid) {
        int row=grid.length;
        int col=grid[0].length;
        int[][] res=new int[row][col];
        //初始化res
        res[0][0]=grid[0][0];
        for(int i=1;i<row;i++){res[i][0]=grid[i][0]+res[i-1][0];}
        for(int i=1;i<col;i++){res[0][i]=grid[0][i]+res[0][i-1];}

        for(int i=1;i<res.length;i++){
            for(int j=1;j<res[0].length;j++){
                res[i][j]=Math.min(res[i][j-1],res[i-1][j])+grid[i][j];
            }
        }
        return res[row-1][col-1];
    }

    public static void main(String[] args) {
        //[1,3,1],[1,5,1],[4,2,1]
       int a= new Practice64().minPathSum(new int[][]{{1,3,1},{1,5,1},{4,2,1}});
        System.out.println(a);
    }
}
